#include "gui/customerviewadddialog.h"
#include "gui/separatorwidget.h"
#include "core/constants.h"
#include "core/customermodel.h"

void CustomerViewAddDialog::initUiComponents()
{
    QFont boldFont;

    mainLayout = new QVBoxLayout;
    fieldsLayout = new QGridLayout;

    headerLabel = new QLabel("Add a Customer");
    QFont font = headerLabel->font();
    font.setBold(true);
    boldFont = font;  // Copy the bold font, for use by other labels

    // The header label should not be bold-faced
    font.setBold(false);
    font.setPointSize(font.pointSize() + HEADER_UPSIZE);
    headerLabel->setFont(font);
    mainLayout->addWidget(headerLabel);

    instructionLabel = new QLabel(
                "Enter your customer's details.");
    mainLayout->addWidget(instructionLabel);

    mainLayout->addWidget(new SeparatorWidget);

    nameLabel = new QLabel("Name");
    nameLabel->setFont(boldFont);
    nameEdit = new QLineEdit;
    fieldsLayout->addWidget(nameLabel, 0, 0);
    fieldsLayout->addWidget(nameEdit, 0, 1);

    addressLabel = new QLabel("Address");
    addressLabel->setFont(boldFont);
    addressEdit = new QLineEdit;
    fieldsLayout->addWidget(addressLabel, 1, 0);
    fieldsLayout->addWidget(addressEdit, 1, 1);

    contactLabel = new QLabel("Contact number");
    contactLabel->setFont(boldFont);
    contactEdit = new QLineEdit;
    fieldsLayout->addWidget(contactLabel, 2, 0);
    fieldsLayout->addWidget(contactEdit, 2, 1);

    noteLabel = new QLabel("Notes");
    noteLabel->setFont(boldFont);
    noteEdit = new QTextEdit;
    noteEdit->setMinimumHeight(100);

    mainLayout->addLayout(fieldsLayout);
    mainLayout->addWidget(noteLabel);
    mainLayout->addWidget(noteEdit);

    mainLayout->addWidget(new SeparatorWidget);

    buttonBox = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel);
    mainLayout->addWidget(buttonBox);
    this->setLayout(mainLayout);
}

void CustomerViewAddDialog::onSaveButtonClicked()
{
    if (nameEdit->text() == "" || addressEdit->text() == ""
            || contactEdit->text() == "" || noteEdit->toPlainText() == "") {
        QMessageBox::critical(this, "Incomplete data", "Please fill in all input boxes to continue.");
        return;
    }

    // Create a new CustomerModel if and only if we're not an edit dialog. If we're
    // an edit dialog, we already have one, assigned to us before we show.
    if (!this->customer) {
        this->customer = new CustomerModel;
    }

    customer->setName(nameEdit->text());
    customer->setAddress(addressEdit->text());
    customer->setContactNumber(contactEdit->text());
    customer->setNote(noteEdit->toPlainText());

    this->accept();
}

/** This function gets the input from the LineEdits and returns them so that
    it can be used for the insert queries. */
CustomerModel * CustomerViewAddDialog::getCustomer()
{
    return this->customer;
}

CustomerViewAddDialog::CustomerViewAddDialog(QWidget *parent) :
    QDialog(parent)
{
    this->setWindowTitle("Add Customer");
    this->setMinimumSize(500, 320);
    this->initUiComponents();

    connect(buttonBox, SIGNAL(accepted()), this, SLOT(onSaveButtonClicked()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

    this->customer = NULL;
}
